package com.example.covid19.FaceUtil;

import cn.hutool.json.JSONUtil;
import com.example.covid19.common.lang.Result;
import com.example.covid19.util.Face;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;

import static com.example.covid19.util.Face.getBytesFromFile;
import static com.example.covid19.util.Face.post;
@Component
@RestController
@RequestMapping("/face")
public class FaceMe {
    @Autowired
    Face face;
    /**识别人脸token*/
    @RequestMapping("/token")
    public Result face(@RequestPart("file") MultipartFile multipartFile) throws Exception {
        String str2;
        str2 = face.upLoad(multipartFile);
        File file = new File(str2);
        byte[] buff = getBytesFromFile(file);
        String url = "https://api-cn.faceplusplus.com/facepp/v3/detect";
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, byte[]> byteMap = new HashMap<>();
        map.put("api_key", "puaPEqTKRAATMQu35U8y3SwQGiLJduX0");
        map.put("api_secret", "vXTB_SEI0P3sZg3UPcJym7bxb6MrAW8p");
        map.put("return_landmark", "1");
        map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
        byteMap.put("image_file", buff);
        byte[] bacd = post(url, map, byteMap);
        String str = new String(bacd);
        System.out.println(JSONUtil.parseObj(str));
        return Result.success(200,"ok",JSONUtil.parseObj(str));
    }

    public String face2(MultipartFile multipartFile) throws Exception {
        String str2;
        str2 = face.upLoad(multipartFile);
        File file = new File(str2);
        byte[] buff = getBytesFromFile(file);
        String url = "https://api-cn.faceplusplus.com/facepp/v3/detect";
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, byte[]> byteMap = new HashMap<>();
        map.put("api_key", "puaPEqTKRAATMQu35U8y3SwQGiLJduX0");
        map.put("api_secret", "vXTB_SEI0P3sZg3UPcJym7bxb6MrAW8p");
        map.put("return_landmark", "1");
        map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
        byteMap.put("image_file", buff);
        byte[] bacd = post(url, map, byteMap);
        String str = new String(bacd);
        System.out.println(JSONUtil.parseObj(str));
        String str3 = face.getter(str);
        return str3;
    }
    @RequestMapping("/faceadd")
    public String faceadd(String token) throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/addface";
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, byte[]> byteMap = new HashMap<>();
        map.put("api_key", "puaPEqTKRAATMQu35U8y3SwQGiLJduX0");
        map.put("faceset_token","285714627291c5130a4840badca617e2");
        map.put("face_tokens",token);
        map.put("api_secret", "vXTB_SEI0P3sZg3UPcJym7bxb6MrAW8p");
        map.put("return_landmark", "1");
        map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
        byte[] bacd = post(url, map, byteMap);
        String str = new String(bacd);
        System.out.println(str);

        return "成功存入set";
    }
    @RequestMapping("/getdetail")
    public String getdetail() throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail";
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, byte[]> byteMap = new HashMap<>();
        map.put("api_key", "puaPEqTKRAATMQu35U8y3SwQGiLJduX0");
        map.put("faceset_token","285714627291c5130a4840badca617e2");
        map.put("api_secret", "vXTB_SEI0P3sZg3UPcJym7bxb6MrAW8p");
        map.put("faceset_token","285714627291c5130a4840badca617e2");
        map.put("return_landmark", "1");
        map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
        byte[] bacd = post(url, map, byteMap);
        String str = new String(bacd);
        System.out.println(str);

        return "成功查找";
    }
    @RequestMapping("/search")
    public Result search(@RequestParam("token")String token) throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/search";
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, byte[]> byteMap = new HashMap<>();
        map.put("api_key", "puaPEqTKRAATMQu35U8y3SwQGiLJduX0");
        map.put("faceset_token","285714627291c5130a4840badca617e2");
        map.put("face_token",token);
        map.put("api_secret", "vXTB_SEI0P3sZg3UPcJym7bxb6MrAW8p");
        map.put("return_landmark", "1");
        map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
        byte[] bacd = post(url, map, byteMap);
        String str = new String(bacd);
        System.out.println(str);

        return Result.success(200,"ok",str);
    }
    public List sea(String token) throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/search";
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, byte[]> byteMap = new HashMap<>();
        map.put("api_key", "puaPEqTKRAATMQu35U8y3SwQGiLJduX0");
        map.put("faceset_token","285714627291c5130a4840badca617e2");
        map.put("face_token",token);
        map.put("api_secret", "vXTB_SEI0P3sZg3UPcJym7bxb6MrAW8p");
        map.put("return_landmark", "1");
        map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
        byte[] bacd = post(url, map, byteMap);
        String str = new String(bacd);
        System.out.println(str);
        return face.getcc(str);
    }
    @RequestMapping("/getset")
    public Result getset() throws Exception {
        String url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets";
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, byte[]> byteMap = new HashMap<>();
        map.put("api_key", "puaPEqTKRAATMQu35U8y3SwQGiLJduX0");
        map.put("face_tokens","68653f80acf4ce8daa83976e3523a9e7,a4386df18c6633946c23ad95da881b5a");
        map.put("api_secret", "vXTB_SEI0P3sZg3UPcJym7bxb6MrAW8p");
        map.put("faceset_token","285714627291c5130a4840badca617e2");
        map.put("return_landmark", "1");
        map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
        byte[] bacd = post(url, map, byteMap);
        String str = new String(bacd);
        System.out.println(str);

        return Result.success(200,"ok",str);
    }
    @RequestMapping("/delete")
    public Result delete(@RequestParam("token") String token) throws Exception {
        String url = " https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface";
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, byte[]> byteMap = new HashMap<>();
        map.put("api_key", "puaPEqTKRAATMQu35U8y3SwQGiLJduX0");
        map.put("face_tokens",token);
        map.put("api_secret", "vXTB_SEI0P3sZg3UPcJym7bxb6MrAW8p");
        map.put("faceset_token","285714627291c5130a4840badca617e2");
        map.put("return_landmark", "1");
        map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
        byte[] bacd = post(url, map, byteMap);
        String str = new String(bacd);
        System.out.println(str);

        return Result.success(200,"ok",str);
    }



}
